package com.xmall.pethouse_api.mapper;

import com.xmall.pethouse_api.pojo.entity.ShopProduct;
import com.xmall.pethouse_api.pojo.entity.ShopCart;
import com.xmall.pethouse_api.pojo.entity.ShopOrder;
import com.xmall.pethouse_api.pojo.entity.ShopOrderItem;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface ShopMapper {
    // 商品相关
    @Select("SELECT * FROM shop_products WHERE status = 1 ORDER BY created_at DESC")
    List<ShopProduct> listProducts();

    @Select("SELECT * FROM shop_products WHERE id = #{id}")
    ShopProduct getProductById(Long id);

    @Insert("INSERT INTO shop_products (name, description, price, stock, image_url, status, created_at, updated_at) VALUES (#{name}, #{description}, #{price}, #{stock}, #{imageUrl}, #{status}, #{createdAt}, #{updatedAt})")
    void insertProduct(ShopProduct product);

    @Update("UPDATE shop_products SET name=#{name}, description=#{description}, price=#{price}, stock=#{stock}, image_url=#{imageUrl}, status=#{status}, updated_at=#{updatedAt} WHERE id=#{id}")
    void updateProduct(ShopProduct product);

    @Delete("DELETE FROM shop_products WHERE id=#{id}")
    void deleteProduct(Long id);

    // 购物车相关
    @Select("SELECT * FROM shop_cart WHERE user_id = #{userId}")
    List<ShopCart> listCartByUserId(Long userId);

    // 新增：购物车VO联表查询
    List<com.xmall.pethouse_api.pojo.vo.CartVO> listCartVOByUserId(Long userId);

    @Select("SELECT * FROM shop_cart WHERE user_id = #{userId} AND product_id = #{productId}")
    ShopCart getCartItem(@Param("userId") Long userId, @Param("productId") Long productId);

    @Insert("INSERT INTO shop_cart (user_id, product_id, quantity, created_at, updated_at) VALUES (#{userId}, #{productId}, #{quantity}, #{createdAt}, #{updatedAt})")
    void insertCartItem(ShopCart cart);

    @Update("UPDATE shop_cart SET quantity=#{quantity}, updated_at=#{updatedAt} WHERE id=#{id}")
    void updateCartItem(ShopCart cart);

    @Delete("DELETE FROM shop_cart WHERE id=#{id}")
    void deleteCartItem(Long id);

    @Delete("DELETE FROM shop_cart WHERE user_id=#{userId}")
    void clearCartByUserId(Long userId);

    // 订单相关（复杂SQL建议用XML实现）
    List<ShopOrder> listOrdersByUserId(Long userId);
    ShopOrder getOrderById(Long id);
    void insertOrder(ShopOrder order);
    void updateOrder(ShopOrder order);
    void deleteOrder(Long id);

    // 订单明细（复杂SQL建议用XML实现）
    List<ShopOrderItem> listOrderItemsByOrderId(Long orderId);
    void insertOrderItem(ShopOrderItem item);
    void deleteOrderItemsByOrderId(Long orderId);
} 